﻿#pragma execution_character_set("utf-8")

#include "mainframeui.h"
#include "ui_mainframeui.h"
#include "iconhelper.h"
#include "serialportset.h"

MainFrameUi::MainFrameUi(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainFrameUi)
{
    ui->setupUi(this);
    this->initForm();
    this->initLeftMain();
    caliWidget = new calibrationWidget;
    connect(caliWidget, SIGNAL(startSignal()), this, SLOT(setStartTrue()));
    connect(caliWidget, SIGNAL(stopSignal()), this, SLOT(setStopTrue()));
    connect(caliWidget, SIGNAL(sendStandardValue(QString)), serialPortSet::Instance()->bytes, SLOT(setStandardValue(QString)));
    ui->verticalLayout_6->addWidget(caliWidget);
    this->initLeftConfig();
    //honglvdeng
    flButton = new frmLightButton;
    ui->vLayoutLight->addWidget(flButton);
    flButton->setMaximumHeight(30);
    //基础设置界面
    baseWidget = new baseSetting;
    ui->baseMainWidget->addWidget(baseWidget);
    //吸阻仪设置界面
    sWidget = new suctionSettingWidget;
    ui->verticalLayout_9->addWidget(sWidget);

    //圆周设置界面
    yzWidget = new yuanzhouSettingWidget;
    ui->vLayoutYz->addWidget(yzWidget);
    //吸阻仪测试界面
    testWidget = new testingMainWidget;
    ui->verticalLayout_11->addWidget(testWidget);
    connect(sWidget, SIGNAL(sendHeaderList(QStringList)), testWidget, SLOT(setTableHeader(QStringList)));
    connect(sWidget, SIGNAL(sendHoldTime(int)), testWidget, SIGNAL(transitHoldTime(int)));

    connect(baseWidget, SIGNAL(sendSuveyor(QString)), testWidget, SLOT(setSuveyor(QString)));
    connect(baseWidget, SIGNAL(sendShifts(QString)), testWidget, SLOT(setShitfs(QString)));
    connect(testWidget, SIGNAL(send2Cali(QString)), caliWidget, SLOT(handleSerialReturn(QString)));//将校准返回传入caliwidget
    //是否开始
    connect(testWidget, SIGNAL(startSignal()), this, SLOT(setStartTrue()));
    connect(testWidget, SIGNAL(stopSignal()), this, SLOT(setStopTrue()));
    //关于界面
    aboutWidget = new about;
    ui->aboutLayout->addWidget(aboutWidget);
    ui->lab2->hide();ui->lab1->hide();
    //查询界面
    searchWidget = new testSearchWidget;
    ui->searchLayout->addWidget(searchWidget);
    ui->lab3->hide();

    connect(baseWidget, SIGNAL(sampleComboxChanged(int)), testWidget, SLOT(setSampleComboxChanged(int)));
    ui->stackedWidget->setCurrentIndex(0);
    //隐藏
    ui->tbtnConfig2->hide();
    ui->tbtnConfig3->hide();
    ui->tbtnMain2->hide();
    ui->tbtnMain3->hide();
}

MainFrameUi::~MainFrameUi()
{
    delete ui;
}

bool MainFrameUi::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick) {
        if (watched == ui->widgetTitle) {
            on_btnMenu_Max_clicked();
            return true;
        }
    }

    return QWidget::eventFilter(watched, event);
}

void MainFrameUi::initForm()
{
    this->setProperty("form", true);
    this->setProperty("canMove", true);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);

    IconHelper::Instance()->setIcon(ui->labIco, QChar(0xF501), 40);
    IconHelper::Instance()->setIcon(ui->btnMenu_Min, QChar(0xf068));
    IconHelper::Instance()->setIcon(ui->btnMenu_Max, QChar(0xf067));
    IconHelper::Instance()->setIcon(ui->btnMenu_Close, QChar(0xf00d));

    //ui->widgetMenu->setVisible(false);
    ui->widgetTitle->installEventFilter(this);
    ui->widgetTitle->setProperty("form", "title");
    ui->widgetTop->setProperty("nav", "top");
    ui->labTitle->setText("综合测试平台");
    ui->labTitle->setFont(QFont("Microsoft Yahei", 20));
    this->setWindowTitle(ui->labTitle->text());

//    ui->stackedWidget->setStyleSheet("QLabel{font:60pt;}");

    //单独设置指示器大小
    int addWidth = 20;
    int addHeight = 10;
    int rbtnWidth = 15;
    int ckWidth = 13;
    int scrWidth = 12;
    int borderWidth = 3;

    QStringList qss;
    qss.append(QString("QComboBox::drop-down,QDateEdit::drop-down,QTimeEdit::drop-down,QDateTimeEdit::drop-down{width:%1px;}").arg(addWidth));
    qss.append(QString("QComboBox::down-arrow,QDateEdit[calendarPopup=\"true\"]::down-arrow,QTimeEdit[calendarPopup=\"true\"]::down-arrow,"
                       "QDateTimeEdit[calendarPopup=\"true\"]::down-arrow{width:%1px;height:%1px;right:2px;}").arg(addHeight));
    qss.append(QString("QRadioButton::indicator{width:%1px;height:%1px;}").arg(rbtnWidth));
    qss.append(QString("QCheckBox::indicator,QGroupBox::indicator,QTreeWidget::indicator,QListWidget::indicator{width:%1px;height:%1px;}").arg(ckWidth));
    qss.append(QString("QScrollBar:horizontal{min-height:%1px;border-radius:%2px;}QScrollBar::handle:horizontal{border-radius:%2px;}"
                       "QScrollBar:vertical{min-width:%1px;border-radius:%2px;}QScrollBar::handle:vertical{border-radius:%2px;}").arg(scrWidth).arg(scrWidth / 2));
    qss.append(QString("QWidget#widget_top>QToolButton:pressed,QWidget#widget_top>QToolButton:hover,"
                       "QWidget#widget_top>QToolButton:checked,QWidget#widget_top>QLabel:hover{"
                       "border-width:0px 0px %1px 0px;}").arg(borderWidth));
    qss.append(QString("QWidget#widgetleft>QPushButton:checked,QWidget#widgetleft>QToolButton:checked,"
                       "QWidget#widgetleft>QPushButton:pressed,QWidget#widgetleft>QToolButton:pressed{"
                       "border-width:0px 0px 0px %1px;}").arg(borderWidth));
    this->setStyleSheet(qss.join(""));

//导航按钮大小设置
    QSize icoSize(114, 44);
    int icoWidth = 85;

    //设置顶部导航按钮
    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }

    ui->btnMain->click();

    ui->widgetLeftMain->setProperty("flag", "left");
    ui->widgetLeftConfig->setProperty("flag", "left");
    //设置左边导航栏按钮大小
    ui->page4->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(60));
    ui->page2->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(60));
}
void MainFrameUi::initLeftMain()
{
//    pixCharMain << 0xf030 << 0xf03e << 0xf247;247可用作吸阻仪
    /*
     * 0xf031 A
     * 0xf032 B
     * 0xf033 I
    */
    pixCharMain << 0xf2A4 << 0xf035 << 0xf036;
    btnsMain << ui->tbtnMain1 << ui->tbtnMain2 << ui->tbtnMain3;

    int count = btnsMain.count();
    for (int i = 0; i < count; i++) {
        btnsMain.at(i)->setCheckable(true);
        btnsMain.at(i)->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btnsMain.at(i), SIGNAL(clicked(bool)), this, SLOT(leftMainClick()));
    }

    IconHelper::Instance()->setStyle(ui->widgetLeftMain, btnsMain, pixCharMain, 15, 35, 25, "left", 4);

    ui->tbtnMain1->click();
}
void MainFrameUi::leftMainClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    int count = btnsMain.count();
    for (int i = 0; i < count; i++) {
        if (btnsMain.at(i) == b) {
            btnsMain.at(i)->setChecked(true);
            btnsMain.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btnsMain.at(i), false)));
        } else {
            btnsMain.at(i)->setChecked(false);
            btnsMain.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btnsMain.at(i), true)));
        }
    }

    ui->lab1->setText(name);//待删除
    QStringList strList;
    strList<<"吸阻仪";
//    strList<<"吸阻仪"<<"单元设置"<<"圆周";
    switch (strList.indexOf(name)) {
    case 0:
        ui->stackedWidget_3->setCurrentIndex(0);
        break;
//    case 1:
//        QMessageBox::warning(NULL, "提示", "功能暂未开放！");
//        ui->stackedWidget_3->setCurrentIndex(1);
//        break;
//    case 2:
//        ui->stackedWidget_3->setCurrentIndex(2);
//        break;
    default:
        return;
    }
}
void MainFrameUi::buttonClick()
{
    if(isStart)
    {
        QMessageBox::warning(NULL, "提示", "正在测试中！");
        return;
    }
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        if (btn == b) {
            btn->setChecked(true);
        } else {
            btn->setChecked(false);
        }
    }

    if (name == "运行测试") {
        ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "单元设置") {
        ui->stackedWidget->setCurrentIndex(1);
    } else if (name == "测试查询") {
        ui->stackedWidget->setCurrentIndex(2);
    } else if (name == "校准设置") {
        ui->stackedWidget->setCurrentIndex(3);
    } else if (name == "关于众沃") {
        ui->stackedWidget->setCurrentIndex(4);
    } else if (name == "用户退出"){
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "确认退出?",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            return;
        case QMessageBox::Yes:
            exit(0);
        default:
            break;
        }
    }
}

void MainFrameUi::on_btnMenu_Min_clicked()
{
    showMinimized();
}

void MainFrameUi::on_btnMenu_Max_clicked()
{
    static bool max = false;
    static QRect location = this->geometry();

    if (max) {
        this->setGeometry(location);
    } else {
        location = this->geometry();
        this->setGeometry(qApp->desktop()->availableGeometry());
    }

    this->setProperty("canMove", max);
    max = !max;
}

void MainFrameUi::on_btnMenu_Close_clicked()
{
    close();
}
void MainFrameUi::initLeftConfig()
{
    pixCharConfig << 0xf2A4 << 0xf035 << 0xf036;
    btnsConfig << ui->tbtnConfig1 << ui->tbtnConfig2 << ui->tbtnConfig3;

    int count = btnsConfig.count();
    for (int i = 0; i < count; i++) {
        btnsConfig.at(i)->setCheckable(true);
        btnsConfig.at(i)->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btnsConfig.at(i), SIGNAL(clicked(bool)), this, SLOT(leftConfigClick()));
    }

//    IconHelper::Instance()->setStyle(ui->widgetLeftConfig, btnsConfig, pixCharConfig, 10, 20, 15, "left", 5);
    IconHelper::Instance()->setStyle(ui->widgetLeftConfig, btnsConfig, pixCharConfig, 15, 35, 25, "left", 4);
    ui->tbtnConfig1->click();
}

void MainFrameUi::leftConfigClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    int count = btnsConfig.count();
    for (int i = 0; i < count; i++) {
        if (btnsConfig.at(i) == b) {
            btnsConfig.at(i)->setChecked(true);
            btnsConfig.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btnsConfig.at(i), false)));
        } else {
            btnsConfig.at(i)->setChecked(false);
            btnsConfig.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btnsConfig.at(i), true)));
        }
    }

    ui->lab2->setText(name);
}
void MainFrameUi::setStartTrue()
{
    isStart = true;
    ui->btnMenu_Close->setDisabled(true);
};
void MainFrameUi::setStopTrue()
{
    isStart = false;
    ui->btnMenu_Close->setDisabled(false);
};
